图像处理: 五种 插值法 | 您所在的位置:网站首页 › 双线性插值 双三次插值 › 图像处理: 五种 插值法 |
Syntax
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst interpolation 选项所用的插值方法INTER_NEAREST最近邻插值INTER_LINEAR双线性插值(默认设置)INTER_AREA使用像素区域关系进行重采样。 它可能是图像抽取的首选方法,因为它会产生无云纹理的结果。 但是当图像缩放时,它类似于INTER_NEAREST方法。INTER_CUBIC4x4像素邻域的双三次插值INTER_LANCZOS48x8像素邻域的Lanczos插值 INTER_NEAREST | 最近邻插值在一维空间中,最近点插值就相当于四舍五入取整。在二维图像中,像素点的坐标都是整数,该方法就是选取离目标点最近的点。 会在一定程度上损失 空间对称性(Alignment),在 RoI Pooling 中使用。 在两个方向分别进行一次线性插值。 在图像处理的时候,我们先根据 srcX = dstX* (srcWidth/dstWidth) srcY = dstY * (srcHeight/dstHeight)来计算目标像素在源图像中的位置,这里计算的srcX和srcY一般都是浮点数,比如 f(1.2, 3.4)这个像素点是虚拟存在的,先找到与它临近的四个实际存在的像素点 (1,3) (2,3) (1,4) (2,4)写成 f(i+u,j+v)的形式,则 u=0.2,v=0.4, i=1, j=3。 f(i+u,j+v) = (1-u)(1-v)f(i,j) + (1-u)vf(i,j+1) + u(1-v)f(i+1,j) + uvf(i+1,j+1)保证了 空间对称性(Alignment),在 RoI Align 中使用。 略。 INTER_CUBIC | 4x4像素邻域的双三次插值略。 INTER_LANCZOS4 | 8x8像素邻域的Lanczos插值在x,y方向分别对相邻的八个点进行插值,也就是计算加权和,所以它是一个8x8的描述子。 Code # coding=utf-8 import cv2 """ INTER_NEAREST | 最近邻插值 INTER_LINEAR | 双线性插值(默认设置) INTER_AREA | 使用像素区域关系进行重采样 INTER_CUBIC | 4x4像素邻域的双三次插值 INTER_LANCZOS4 | 8x8像素邻域的Lanczos插值 """ if __name__ == '__main__': img = cv2.imread("girl.jpg") height, width = img.shape[:2] # 缩小图像 size = (int(width*0.8), int(height*0.7)) shrink_NEAREST = cv2.resize(img, size, interpolation=cv2.INTER_NEAREST) shrink_LINEAR = cv2.resize(img, size, interpolation=cv2.INTER_LINEAR) shrink_AREA = cv2.resize(img, size, interpolation=cv2.INTER_AREA) shrink_CUBIC = cv2.resize(img, size, interpolation=cv2.INTER_CUBIC) shrink_LANCZOS4 = cv2.resize(img, size, interpolation=cv2.INTER_LANCZOS4) # 放大图像 fx = 1.2 fy = 1.1 enlarge_NEAREST = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_NEAREST) enlarge_LINEAR = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_LINEAR) enlarge_AREA = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_AREA) enlarge_CUBIC = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_CUBIC) enlarge_LANCZOS4 = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_LANCZOS4) # 保存图像 cv2.imwrite("shrink_NEAREST.jpg", shrink_NEAREST) cv2.imwrite("shrink_LINEAR.jpg", shrink_LINEAR) cv2.imwrite("shrink_AREA.jpg", shrink_AREA) cv2.imwrite("shrink_CUBIC.jpg", shrink_CUBIC) cv2.imwrite("shrink_LANCZOS4.jpg", shrink_LANCZOS4) cv2.imwrite("enlarge_NEAREST.jpg", enlarge_NEAREST) cv2.imwrite("enlarge_LINEAR.jpg", enlarge_LINEAR) cv2.imwrite("enlarge_AREA.jpg", enlarge_AREA) cv2.imwrite("enlarge_CUBIC.jpg", enlarge_CUBIC) cv2.imwrite("enlarge_LANCZOS4.jpg", enlarge_LANCZOS4) Demo原图像: 肉眼基本看不出差别。 最近邻插值: 双线性插值(默认设置): 使用像素区域关系进行重采样: 4x4像素邻域的双三次插值: 8x8像素邻域的Lanczos插值: 最近邻插值: 双线性插值(默认设置): 使用像素区域关系进行重采样: 4x4像素邻域的双三次插值: 8x8像素邻域的Lanczos插值: |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |